package com.lwgk.basic;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.lwgk.compinterface.BackCall;
import com.lwgk.constant.ConstantUtils;
import com.lwgk.mvp.view.SplashActivity;
import com.lwgk.utils.AppManagerUtils;
import com.lwgk.utils.DialogUtils;

import java.util.ArrayList;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/11
 * --------------------------------------------------------------------------
 * 描述：所有界面公共的基础类，主要是界面逻辑层
 * 注意事项：掌握，对外的权限上到最低，不建议用public 尽量用protected，有单个权限请求，多个权限请求
 */
public abstract class AllBasicActivity extends AppCompatActivity {
    //app的基本权限
    private String[] permissionss = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
    };
    private ArrayList<Object> mPermissionList;
    private Dialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //1.权限的申请
        //（0.1）权限组
        //  getListPermissions();

        //2,请求单个权限
        //        getSinglePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);

        //添加所有的任务栈，用于登录界面105时，结束当前界面，返回上一个页面
        AppManagerUtils.getInstance().addActivity(AllBasicActivity.this);
    }

    /**
     * 请求权限组
     */
    protected void getListPermissions() {
        // 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
        if (mPermissionList == null) {
            mPermissionList = new ArrayList<>();
        } else {
            mPermissionList.clear();
        }
        //检查版本是否大于M也就是5.0的手机
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < permissionss.length; i++) {
                //检查是否获取了权限
                if (ContextCompat.checkSelfPermission(AllBasicActivity.this, permissionss[i]) != PackageManager.PERMISSION_GRANTED) {
                    mPermissionList.add(permissionss[i]);
                }
            }
            if (!mPermissionList.isEmpty()) {
                //将List转为数组
                String[] permissionarrs = mPermissionList.toArray(new String[mPermissionList.size()]);
                ActivityCompat.requestPermissions(AllBasicActivity.this, permissionarrs, ConstantUtils.MY_PERMISIONLIST);
            }
        }

    }

    /**
     * @param permision 需要的单个权限
     */
    protected boolean getSinglePermission(String permision) {
        //检查版本是否大于M也就是5.0的手机
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, permision) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{permision},
                        ConstantUtils.MY_SINGLEPERMISION);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }


    }

    //权限回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (permissions.length == 0) {
            Intent intent = new Intent(
                    Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
            finish();
            return;
        }
        if (grantResults.length == 0) {
            Intent intent = new Intent(
                    Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
            finish();
            return;
        }

        //单项回调
        if (requestCode == ConstantUtils.MY_SINGLEPERMISION) {
            if (grantResults.length == 0) {
                return;
            }
            if (permissions.length == 0) {
                return;
            }
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {

                if (mDialog == null)
                    mDialog = DialogUtils.showTitleDoubleLiftSelect(AllBasicActivity.this, "权限管理", "需要读取内存卡权限才能进行其他操作", "去设置", "取消", true, new BackCall() {
                        @Override
                        public void deal(int which, int postion) {
                            super.deal(which, postion);
                            switch (postion) {
                                case 0:
                                    Intent intent = new Intent(
                                            Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                    intent.setData(Uri.parse("package:" + getPackageName()));
                                    startActivity(intent);
                                    finish();
                                    break;
                                case 1:
                                    finish();
                                    break;
                            }
                        }
                    });
                mDialog.show();
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }
}
